package com.api.department.preview;

import java.io.IOException;
import java.io.InputStream;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.test.util.IOUtils;
import com.zyncro.api.IDepartmentApiService;
import com.zyncro.api.exceptions.ZyncroApiException;

@ContextConfiguration(locations = "classpath:/department/department-toedit.xml")
public class Preview extends AbstractApiTest {

	private static final int DEFAULT_WITH = 50;
	private static final int DEFAULT_HEIGHT = 50;

	private static final int PROPORTIONS_MODE = 0;
	private static final int STRECH_MODE = 1;
	private static final int CROP_MODE = 2;

	@Value("#{department_toEdit[owner].appId}")
	private String userUrn;

	@Value("#{department_toEdit[owner].password}")
	private String userPassword;

	@Value("#{department_toEdit.urn}")
	private String iddepartment;

	@Value("#{department_toEdit.imagePngUrn}")
	private String imagePngUrn;

	@Value("#{department_toEdit.imageBmpUrn}")
	private String imageBmpUrn;

	@Value("#{department_toEdit.imageJpgUrn}")
	private String imageJpgUrn;

	@Value("#{department_toEdit.imageGifUrn}")
	private String imageGifUrn;

	@Value("#{department_toEdit.imageTifUrn}")
	private String imageTifUrn;

	@BeforeClass
	protected void loginUser() throws Exception {

		getApi().login(userUrn, userPassword);
	}

	@Test
	public void testPreviewPngProportions() throws ZyncroApiException {

		int width = DEFAULT_WITH;
		int height = DEFAULT_HEIGHT;

		int mode = PROPORTIONS_MODE;

		getApi().getService(IDepartmentApiService.class).preview(iddepartment, imagePngUrn, getApi().getAccessKey(), width, height, mode);
	}

	@Test
	public void testPreviewPngStrech() throws ZyncroApiException {

		int width = DEFAULT_WITH;
		int height = DEFAULT_HEIGHT;

		int mode = STRECH_MODE;

		getApi().getService(IDepartmentApiService.class).preview(iddepartment, imagePngUrn, getApi().getAccessKey(), width, height, mode);
	}

	@Test
	public void testPreviewPngCrop() throws ZyncroApiException {

		int width = DEFAULT_WITH;
		int height = DEFAULT_HEIGHT;

		int mode = CROP_MODE;

		getApi().getService(IDepartmentApiService.class).preview(iddepartment, imagePngUrn, getApi().getAccessKey(), width, height, mode);
	}

	@Test
	public void testPreviewBmp() throws ZyncroApiException, IOException {

		int width = DEFAULT_WITH;
		int height = DEFAULT_HEIGHT;

		int mode = 0;

		InputStream is = getApi().getService(IDepartmentApiService.class).preview(iddepartment, imageBmpUrn, getApi().getAccessKey(),
				width, height, mode);
		IOUtils.saveStreamToFile(is, "previewBmp.bmp");
	}

	@Test
	public void testPreviewJpg() throws ZyncroApiException, IOException {

		int width = DEFAULT_WITH;
		int height = DEFAULT_HEIGHT;

		int mode = 0;
		getApi().getService(IDepartmentApiService.class).preview(iddepartment, imageJpgUrn, getApi().getAccessKey(), width, height, mode);
	}

	@Test
	public void testPreviewGif() throws ZyncroApiException, IOException {

		int width = DEFAULT_WITH;
		int height = DEFAULT_HEIGHT;

		int mode = 0;

		getApi().getService(IDepartmentApiService.class).preview(iddepartment, imageGifUrn, getApi().getAccessKey(), width, height, mode);
	}

	@Test
	public void testPreviewTif() throws ZyncroApiException {

		int width = DEFAULT_WITH;
		int height = DEFAULT_HEIGHT;

		int mode = 0;

		getApi().getService(IDepartmentApiService.class).preview(iddepartment, imageTifUrn, getApi().getAccessKey(), width, height, mode);
	}
}
